package com.shemy.fileservice.config;

import com.shemy.fileservice.domain.FileEntity;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
 * @Description redis配置
 * @Author Dzsom
 * @Date 2018-10-31 14:55
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, FileEntity> fileRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, FileEntity> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<FileEntity> serializer = new Jackson2JsonRedisSerializer<>(FileEntity.class);
        template.setDefaultSerializer(serializer);
        return template;
    }

    @Primary
    @Bean
    public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setUsePrefix(true);
        return cacheManager;
    }

    @Bean
    public RedisCacheManager fileRedisCacheManager(RedisTemplate<Object, FileEntity> fileRedisTemplate){
        RedisCacheManager cacheManager = new RedisCacheManager(fileRedisTemplate);
//      使用前缀
        cacheManager.setUsePrefix(true);
        return cacheManager;
    }
}
